package com.css.dzj.zfjg.modules.admpenalty.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.*;
import com.css.dzj.zfjg.modules.admpenalty.entity.AdmPenaltyEntity;
import com.css.dzj.zfjg.modules.admpenalty.service.AdmPenaltyService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 行政处罚Controller
 * @Author WT
 * @Date 2020/5/25
 */
@RestController
@RequestMapping("/penalty")
public class AdmPenaltyController {

    @Autowired
    private AdmPenaltyService admPenaltyService;
    @Autowired
    private BaseService baseService;
    @Autowired
    HttpServletRequest request;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<AdmPenaltyEntity> admPenaltyResults = admPenaltyService.queryPage(pageRequest);
            baseService.logSuccess(3, "查询行政处罚成功", "2");
            return R.ok().put("data", admPenaltyResults);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "查询行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String id){
        try {
            AdmPenaltyEntity admPenaltyEntity = admPenaltyService.findById(id);
            baseService.logSuccess(3, "通过id查询行政处罚成功", "2");
            return R.ok().put("data", admPenaltyEntity);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id查询行政处罚失败","2");
            return R.error(e.getMessage());
        }

    }


    /**
     * 保存提交
     */
    @PostMapping("/save")
    public R save(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        try {
            if(StringUtils.isEmpty(admPenaltyEntity)) {
                return R.error("行政处罚信息不能为空");
            }
            admPenaltyService.add(admPenaltyEntity);
            baseService.logSuccess(3, "添加行政处罚成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "添加行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 暂存
     */
    @PostMapping("/storage")
    public R storage(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        try {
            if(StringUtils.isEmpty(admPenaltyEntity)) {
                return R.error("行政处罚信息不能为空");
            }
            admPenaltyService.storage(admPenaltyEntity);
            baseService.logSuccess(3, "暂存行政处罚成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "暂存行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 提交
     */
    @PostMapping("/submit")
    public R submit(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        try {
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setUpdateTime(new Date());
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_TRUE);
            admPenaltyService.update(oldAdmPenaltyEntity);
            baseService.logSuccess(3, "提交行政处罚成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "提交行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 退回
     */
    @PostMapping("/back")
    public R back(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        try {
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setBackTime(new Date());
            oldAdmPenaltyEntity.setTaskStatus(Constant.ISSUBMIT_BACK);
            oldAdmPenaltyEntity.setBackUser(ServiceUtil.getLoginName(request));
            admPenaltyService.update(oldAdmPenaltyEntity);
            baseService.logSuccess(3, "退回行政处罚成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "退回行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AdmPenaltyEntity admPenaltyEntity){
        try {
            AdmPenaltyEntity oldAdmPenaltyEntity = admPenaltyService.findById(admPenaltyEntity.getId());
            BeanUtil.copyBeanNotNullToBean(admPenaltyEntity, oldAdmPenaltyEntity);
            oldAdmPenaltyEntity.setUpdateTime(new Date());
            admPenaltyService.update(oldAdmPenaltyEntity);
            baseService.logSuccess(3, "修改行政处罚成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "修改行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String id){

        try {
            if(admPenaltyService.delete(id)){
                baseService.logSuccess(3, "删除行政处罚成功", "2");
                return R.ok("操作成功!");
            }else{
                baseService.logFailed(3, "删除行政处罚失败","2");
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "删除行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }


    /**
     *多条件查询行政处罚
     */
    @GetMapping("/xzcfList")
    public R getXzcfist( @RequestParam(required = false) String enforceName,
                         @RequestParam(required = false) String enforceOrganizationSite,
                         @RequestParam(required = false) String enforceObject,
                         @RequestParam(required = false) String breakFact,
                         @RequestParam(required = false) String caseSource,
                         @RequestParam(required = false) String taskStatus,
                         @RequestParam(required = false) String disposeResult,
                         @RequestParam(required = false) String penaltyCategory,
                         @RequestParam(required = false) String enforceOrganization,
                         @RequestParam(required = false) String caseCategory,
                         @RequestParam(required = false) String releaseStartTime,
                         @RequestParam(required = false) String releaseEndTime,
                         @RequestParam(required = false) Integer pageSize, @RequestParam Integer pageNo) {
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<AdmPenaltyEntity> admPenaltyResults = admPenaltyService.getXzcfist(enforceName,enforceOrganizationSite,enforceObject,breakFact,
                                                               caseSource,taskStatus,disposeResult,penaltyCategory,enforceOrganization,caseCategory,
                                                                releaseStartTime,releaseEndTime,pageRequest);
            baseService.logSuccess(3, "多条件查询行政处罚成功", "2");
            return R.ok().put("data", admPenaltyResults);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "多条件查询行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出选中行数据
     */
    @GetMapping("/exportByIds")
    public R exportHolidayToExcel(HttpServletResponse response, @RequestParam String ids) {
        try {
            List<AdmPenaltyEntity> admPenaltyEntities = admPenaltyService.queryByIds(ids);
            admPenaltyService.exportAdmPenaltyToExcel(response, admPenaltyEntities);
            baseService.logSuccess(3, "导出行政处罚成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "导出行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据条件导出数据
     */
    @GetMapping("/export")
    public R export( HttpServletResponse response,
                     @RequestParam(required = false) String enforceName,
                     @RequestParam(required = false) String enforceOrganizationSite,
                     @RequestParam(required = false) String enforceObject,
                     @RequestParam(required = false) String breakFact,
                     @RequestParam(required = false) String caseSource,
                     @RequestParam(required = false) String taskStatus,
                     @RequestParam(required = false) String disposeResult,
                     @RequestParam(required = false) String penaltyCategory,
                     @RequestParam(required = false) String enforceOrganization,
                     @RequestParam(required = false) String caseCategory,
                     @RequestParam(required = false) String releaseStartTime,
                     @RequestParam(required = false) String releaseEndTime) {
        try{
          List<AdmPenaltyEntity> admPenaltyResults = admPenaltyService.queryByParams(enforceName,enforceOrganizationSite,enforceObject,breakFact,
                                                      caseSource,taskStatus,disposeResult,penaltyCategory,enforceOrganization,caseCategory,
                                                      releaseStartTime,releaseEndTime);
            admPenaltyService.exportAdmPenaltyToExcel(response, admPenaltyResults);
            baseService.logSuccess(3, "多条件导出行政处罚成功", "2");
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3, "多条件导出行政处罚失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 模板下载
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(){
        try {
            String url = "src/main/resources/static/FileTemplate/新增防震减灾行政处罚案件库模板.xlsx";
            String fileName = "新增防震减灾行政处罚案件库模板.xlsx";
            PlatformFileUtil.download(fileName,url);
            baseService.logSuccess(3, "行政处罚模板下载成功", "2");
        }catch (Exception e){
            baseService.logFailed(3, "行政处罚模板下载失败","2");


        }
    }

    /**
     *导入
     */
    @PostMapping("/deptExcel")
    public R uploadFile(@RequestParam("file") MultipartFile file,HttpServletResponse response) {

        try {
            String returnMsg = admPenaltyService.uploadFile(file,response);
            baseService.logSuccess(3, "行政处罚导入成功", "2");
            return R.ok().put("data", returnMsg);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "行政处罚导入失败","2");
            return R.error(e.getMessage());
        }
    }

}
